﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using QLKhachSan.DTO;
using System.Data.SqlClient;
using QLKhachSan.DAO;
using QLKhachSan.BUS;
using DevExpress.XtraEditors;

namespace QLKhachSan.GUI
{
    public partial class fDatTiec : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public fDatTiec()
        {
            InitializeComponent();
        }
        private int r;
        public static int maDatTiec;
        private fInBaoCao _fInBaoCao = new fInBaoCao();
        private void fDatTiec_Load(object sender, EventArgs e)
        {
            // Ten Nhan Vien
            DataTable dta_nv = DAO_NhanVien.hienthiDanhSachNhanVienTheoMaNV(int.Parse(fLogin.maNV));
            textBoxTenNV.Text = dta_nv.Rows[0]["TenNV"].ToString();
            textBoxTenNV.Enabled = false;
            // Ten Khach Hang            
            textBoxTenKH.Enabled = false;          
            DataTable dta_kh = BUS_KhachHang.hienthiDanhSachKhachHang();
            for (int j = 0; j < dta_kh.Rows.Count; j++)
            {
                if (int.Parse(dta_kh.Rows[j]["MaKH"].ToString()) == fDanhSachPhong.maKH)
                {
                    textBoxTenKH.Text = dta_kh.Rows[j]["TenKH"].ToString();
                    break;
                }
            }
            // Chi Tiet Dat Tiec
            textBoxDonGia.Enabled = false;
            textBoxDonGia.ResetText();
            textBoxGhiChu.Enabled = false;
            textBoxGhiChu.ResetText();
            textBoxSL.Enabled = false;
            textBoxSL.ResetText();
            textBoxTenMon.Enabled = false;
            textBoxTenMon.ResetText();
            textBoxThamMy.Enabled = false;
            textBoxThamMy.ResetText();
            dateTimePickerThoiGian.Enabled = false;
         
            simpleButtonHuy.Enabled = false;
            simpleButtonLuu.Enabled = false;
            simpleButtonThem.Enabled = true;
            simpleButtonXoa.Enabled = true;
           
            

        }

        private void textBoxSL_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
                e.Handled = true;
        }

        private void textBoxDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
                e.Handled = true;
        }

        private void simpleButtonThem_Click(object sender, EventArgs e)
        {
            textBoxDonGia.Enabled = true;
            textBoxDonGia.ResetText();
            textBoxGhiChu.Enabled = true;
            textBoxGhiChu.ResetText();
            textBoxSL.Enabled = true;
            textBoxSL.ResetText();
            textBoxTenMon.Enabled = true;
            textBoxTenMon.ResetText();
            textBoxThamMy.Enabled = true;
            textBoxThamMy.ResetText();
            dateTimePickerThoiGian.Enabled = true;
           

            simpleButtonHuy.Enabled = true;
            simpleButtonLuu.Enabled = true;
            simpleButtonThem.Enabled = false;
            simpleButtonXoa.Enabled = false;
            
        }

        private void simpleButtonSua_Click(object sender, EventArgs e)
        {
           if(textBoxTenMon.Text !="")
           {
            textBoxDonGia.Enabled = true;         
            textBoxGhiChu.Enabled = true;
            textBoxSL.Enabled = true;         
            textBoxTenMon.Enabled = false;           
            textBoxThamMy.Enabled = true;            
            dateTimePickerThoiGian.Enabled = true;
        
            simpleButtonHuy.Enabled = true;
            simpleButtonLuu.Enabled = true;
            simpleButtonThem.Enabled = false;
            simpleButtonXoa.Enabled = false;
            
           }
           else
           {
               MessageBox.Show("Cần Chọn món cần xóa  ", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
        }

        private void simpleButtonHuy_Click(object sender, EventArgs e)
        {
            textBoxDonGia.Enabled = false;
            textBoxDonGia.ResetText();
            textBoxGhiChu.Enabled = false;
            textBoxGhiChu.ResetText();
            textBoxSL.Enabled = false;
            textBoxSL.ResetText();
            textBoxTenMon.Enabled = false;
            textBoxTenMon.ResetText();
            textBoxThamMy.Enabled = false;
            textBoxThamMy.ResetText();
            dateTimePickerThoiGian.Enabled = false;
           

            simpleButtonHuy.Enabled = false;
            simpleButtonLuu.Enabled = false;
            simpleButtonThem.Enabled = true;
            simpleButtonXoa.Enabled = true;
          
            

        }

        private void simpleButtonLuu_Click(object sender, EventArgs e)
        {
            if(textBoxDonGia.Text.Trim() !="" && textBoxGhiChu.Text.Trim() !="" && textBoxSL.Text.Trim() != "" &&
                textBoxThamMy.Text.Trim() !="" && textBoxDonGia.Text.Trim() !="")
            {
               
                    dataGridViewDS.Rows.Add(textBoxTenMon.Text, textBoxThamMy.Text, textBoxSL.Text, dateTimePickerThoiGian.Value, textBoxGhiChu.Text, textBoxDonGia.Text);
                    MessageBox.Show("Thêm Thành Công ", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    simpleButtonHuy_Click(sender, e);
                
               
            }
            else
            {
                 MessageBox.Show("Cần điền thông tin đầy Đủ!!!???", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void simpleButtonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridViewDS_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                r = dataGridViewDS.CurrentCell.RowIndex;                
                textBoxTenMon.Text = dataGridViewDS.Rows[r].Cells["TenMonAn"].Value.ToString();
                textBoxThamMy.Text = dataGridViewDS.Rows[r].Cells["ThamMy"].Value.ToString();
                textBoxSL.Text = dataGridViewDS.Rows[r].Cells["SoLuong"].Value.ToString();
                textBoxGhiChu.Text =  dataGridViewDS.Rows[r].Cells["GhiChu"].Value.ToString();
                textBoxDonGia.Text =  dataGridViewDS.Rows[r].Cells["DonGia"].Value.ToString();
                dateTimePickerThoiGian.Text = dataGridViewDS.Rows[r].Cells["ThoiGian"].Value.ToString();              

            }
            catch (Exception)
            {
            }
        }

        private void simpleButtonXoa_Click(object sender, EventArgs e)
        {           
            if (textBoxTenMon.Text != "")
            {
                if (MessageBox.Show("Bạn có Chắc chắn muốn xóa???", "Note !", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if(r  < dataGridViewDS.Rows.Count )
                    {
                        dataGridViewDS.Rows.RemoveAt(r);
                        simpleButtonHuy_Click(sender, e);
                    }
                }
            }
            else
            {
                MessageBox.Show("Chưa chọn Món cần Xóa!!!???", "Thông Báo !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void simpleButtonLap_Click(object sender, EventArgs e)
        {
          
            if (dataGridViewDS.RowCount >1)
            {
                if (MessageBox.Show("Bạn có Chắc chắn muốn Lập Hóa đơn???", "Note !", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    // Them Dac Tiec
                    DTO_DatTiec dto_dt = new DTO_DatTiec();
                    if (checkBoxDaTra.Checked == true)
                    {
                        dto_dt.DaTra = "Rồi"; 
                    }
                        else
                        {
                            dto_dt.DaTra = "Chưa";
                        }
                    dto_dt.MaKH = fDanhSachPhong.maKH;                    
                    dto_dt.MaNV = int.Parse(fLogin.maNV);                    
                    BUS_DatTiec.themDatTiec(dto_dt);                    
                    DTO_CTDatTiec dto_ctdt = new DTO_CTDatTiec();
                    DataTable dta_dt = BUS_DatTiec.hienthiDanhSachDatTiec();
                    dto_ctdt.MaDatTiec = int.Parse(dta_dt.Rows[dta_dt.Rows.Count - 1]["MaDatTiec"].ToString());
                    maDatTiec = int.Parse(dta_dt.Rows[dta_dt.Rows.Count - 1]["MaDatTiec"].ToString());
                    for (int i = 0; i < dataGridViewDS.Rows.Count - 1 ; i++)
                    {
                        dto_ctdt.TenMon = dataGridViewDS.Rows[i].Cells["TenMonAn"].Value.ToString();
                        dto_ctdt.ThamMy = dataGridViewDS.Rows[i].Cells["ThamMy"].Value.ToString();
                        dto_ctdt.SoLuong = int.Parse(dataGridViewDS.Rows[i].Cells["SoLuong"].Value.ToString());
                        dto_ctdt.ThoiGian = DateTime.Parse(dataGridViewDS.Rows[i].Cells["ThoiGian"].Value.ToString());
                        dto_ctdt.DonGia = int.Parse(dataGridViewDS.Rows[i].Cells["DonGia"].Value.ToString());
                        dto_ctdt.GhiChu = dataGridViewDS.Rows[i].Cells["GhiChu"].Value.ToString();
                        BUS_CTDatTiec.themCTDatTiec(dto_ctdt);                        
                    }
                    MessageBox.Show("Đã Lập Hóa Đơn Đặt Tiệc Thành công và In, Đóng Form Lại ", "Thông Báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    _fInBaoCao.Close();
                    _fInBaoCao = new fInBaoCao();
                    _fInBaoCao.MdiParent = fMain.ActiveForm;
                    _fInBaoCao.Show();

                }
            }
            else 
            {
                MessageBox.Show("Chưa có Món ăn để lập Hóa đơn!???", "Lỗi !", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}